package com.nisco.nio.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketTest {
    public static void main(String[] args)
        throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(inputStream));

        String line = "";
        while (!"".equals(line = bufferedInputStream.readLine())) {
            System.out.println(line);
        }

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
        outputStream.write("<html><body><a href='http://www.baidu.com'>i am baiu.com welcome you!</a></body></html>".getBytes());
        outputStream.flush();

        inputStream.close();
        bufferedInputStream.close();
        outputStream.close();
        socket.close();
        serverSocket.close();

    }
}
